package web.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import web.entity.LoginUser;
import web.entity.User;
import web.service.UserService;
import web.utils.ResponseResult;

import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 防止热座篡改密码,需要 验证旧密码,
     * @param user //todo 待定
     * @return 修改结果
     */
    @PostMapping("/changepwd")
    public ResponseResult changepwd(@RequestBody User user){
        return new ResponseResult(200,"占位响应");
    }

    /**
     * 查询自己的个人信息
     * @return 查询结果
     */
    @GetMapping("/detail")
    public ResponseResult getDetail(){
        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
        Long id = user.getUser().getId();
        HashMap userObj = userService.getDetail(Long.toString(id));
        return new ResponseResult(200,userObj);
    }

    /**
     * 修改自己的个人信息
     * @param user 用户模型
     * @return 修改结果
     */
    @PostMapping("/detail")
    public ResponseResult setDetail(@RequestBody User user){
        return new ResponseResult(200,"占位响应");
    }

}
